package cn.gongwebo.payview.widget.dialog;


import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.gongwebo.payview.R;
import cn.gongwebo.payview.adapter.KeyBoardAdapter;
import cn.gongwebo.payview.util.ToastUtil;
import cn.gongwebo.payview.widget.PayView;

/**
 * 支付的fragment
 * A simple {@link Fragment} subclass.
 */
public class PayFragment extends DialogFragment {

    @BindView(R.id.payview)
    PayView      mPayview;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.tv_money)
    TextView     mTvMoney;
    @BindView(R.id.ll_key)
    LinearLayout mLlKey;
    @BindView(R.id.fl_close)
    FrameLayout  mFlClose;
    private             StringBuffer sb    = new StringBuffer();
    public static final String       MONEY = "money";
    private boolean keyHide;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(STYLE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_pay, container, false);
        ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        String money = bundle.getString(MONEY);
        mTvMoney.setText(money);
        init();
        return view;
    }

    private void init() {
        mRv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            list.add(i);
        }
        list.add(KeyBoardAdapter.EMPTY);
        list.add(0);
        list.add(KeyBoardAdapter.DELETE);
        KeyBoardAdapter adapter = new KeyBoardAdapter(list, getActivity());
        mRv.setAdapter(adapter);
        adapter.setKeyBoardAdapterListener(new KeyBoardAdapter.KeyBoardAdapterListener() {
            @Override
            public void click(int code) {
                if (code == KeyBoardAdapter.EMPTY) {

                } else if (code == KeyBoardAdapter.DELETE) {
                    int importCount = mPayview.getImportCount();
                    if (importCount >= 1) {
                        mPayview.setImportCount(importCount - 1);
                        sb.deleteCharAt(sb.length() - 1);
                    }
                } else {
                    int importCount = mPayview.getImportCount();
                    mPayview.setImportCount(importCount + 1);
                    sb.append(String.valueOf(code));
                    // 全部输入完成
                    if (sb.length() == PayView.COUNT) {
                        ToastUtil.show(getActivity(), sb.toString());
                        dismiss();
                    }
                }
            }
        });
    }

    /**
     * FragmentDialog宽高问题
     */
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        Window window = dialog.getWindow();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.anim_dialog);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            window.setBackgroundDrawableResource(android.R.color.transparent);
        }
    }


    @OnClick({R.id.iv_close, R.id.fl_close, R.id.payview})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                // 测试提交
                dismiss();
                break;
            case R.id.fl_close:
                if (!keyHide) {
                    Animation exit = AnimationUtils.loadAnimation(getActivity(), R.anim.exit_anim);
                    mLlKey.setAnimation(exit);
                    mLlKey.startAnimation(exit);
                    exit.setFillAfter(true);
                    keyHide = true;
                    // 影藏的时候要等动画执行完
                    exit.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            // 因为是补间动画所以把控件隐藏
                            mRv.setVisibility(View.GONE);
                            mFlClose.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                }
                break;
            case R.id.payview:
                if (keyHide) {
                    Animation enter = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_anim);
                    mLlKey.setAnimation(enter);
                    mLlKey.startAnimation(enter);
                    enter.setFillAfter(true);
                    keyHide = false;
                    // 因为是补间动画所以把控件显示
                    mRv.setVisibility(View.VISIBLE);
                    mFlClose.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

}
